﻿Ext.define('page.Consume.Mod', {
    extend: 'Kwan.ex.ModuleInstance',
    metas: [
        { name: "OrderID", label: "订单号", sort: 1, ex_list: { width: 150 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "EntityID", label: "消费单号", sort: 2, ex_list: { width: 150 }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.tipSex, def: 0 }, cfg: { labelWidth: 70, width: 250}} },
        { name: "Item", label: "项目名称", sort: 3, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "Nett", label: "实际单价", sort: 4, ex_list: { width: 150 }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.tipIDType, def: 0 }, cfg: { labelWidth: 70, width: 250}} },
        { name: "Price", label: "原单价", sort: 5, ex_edit: { width: 250} },
        { name: "Total", label: "总数", sort: 6, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "CreatedDate", label: "消费时间", sort: 7, ex_list: { width: 150, xtype: 'datecolumn', format: 'Y-m-d H:i:s' }, ex_edit: { width: 250} },
        { name: "UserName", label: "操作员", sort: 8, ex_list: { width: 150 }, ex_edit: { width: 250} },
        { name: "Status", label: "状态", sort: 9, ex_list: { width: 150, renderer: Kwan.colorCsStatus} }
	    ],
    cfg: { start: 0, limit: 20, facade: 'facade.axd?s_action=Consume', def: 'Consume' },
    getGrid: function () {
        var me = this,
            sorters = [{ property: 'OrderID', direction: 'DESC'}];
        Ext.apply(Ext.form.field.VTypes, {
            daterange: function (val, field) {
                var date = field.parseDate(val);

                if (!date) {
                    return false;
                }
                if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
                    var start = Ext.getCmp(field.startDateField);
                    start.setMaxValue(date);
                    start.validate();
                    this.dateRangeMax = date;
                }
                else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
                    var end = Ext.getCmp(field.endDateField);
                    end.setMinValue(date);
                    end.validate();
                    this.dateRangeMin = date;
                }
                return true;
            },
            daterangeText: 'Start date must be less than end date'
        });
        var ds = Ext.create('Ext.data.Store', {
            autoLoad: true,
            pageSize: me.cfg.limit,
            remoteSort: true,
            remoteFilter: true,
            groupField: 'OrderID',
            fields: Kwan.createModel(me.metas),
            sorters: [{ property: 'EntityID', direction: 'DESC'}],
            proxy: { type: 'ajax', url: me.cfg.facade + '&s_method=list', reader: { type: 'json', root: 'data.data', totalProperty: 'data.total' }
            }
        });
        var groupingFeature = Ext.create('Ext.grid.feature.GroupingSummary', {
            groupHeaderTpl: '订单号：{name} (共{[values.rows.length]}项)', hideGroupedHeader: true
        });
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpConsume',
            region: 'center',
            store: ds,
            columns: Kwan.createColumn(this.metas, true),
            features: [{
                ftype: 'groupingsummary',
                hideGroupedHeader: true,
                groupHeaderTpl: '订单号：{name} (共{[values.rows.length]}项)'
            }],
            getQuery: function (clear) {
                var roomNO = Ext.getCmp('qRoomNO').getValue(),
                    startDate = Ext.util.Format.date(Ext.getCmp('qStartDate').getValue(), 'Y-m-d'),
                    endDate = Ext.util.Format.date(Ext.getCmp('qEndDate').getValue(), 'Y-m-d');
                if (clear) {
                    startDate = endDate = roomNO = '';
                }
                if (roomNO.length > 0 || startDate.length > 0|| endDate.length > 0) {
                    var val = "";
                    val += (roomNO.length > 0 ? roomNO : "") + ",";
                    val += (startDate.length > 0 ? startDate : "") + ",";
                    val += (endDate.length > 0 ? endDate : "") + ",";
                    this.store.proxy.extraParams = { query1: 'tag=' + val };
                } else { this.store.proxy.extraParams = null; }
                this.store.load();
                this.filters.bindStore(this.store);
            },
            operBar: [
                    { xtype: 'hidden', id: 'qOrderID', name: 'qOrderID' },
                    { width: 100, emptyText: " 搜索房间号..", xtype: 'textfield', id: 'qRoomNO', name: 'qRoomNO' }, '-',
                    { xtype: 'datefield', width: 120, emptyText: '开始日期', id: 'qStartDate', name: 'qStartDate', vtype: 'daterange', endDateField: 'qEndDate', format: 'Y年m月d日',
                        listeners: { afterrender: function () { this.setValue(new Date()); } }
                    },
                    { xtype: 'datefield', width: 120, emptyText: '结束日期', id: 'qEndDate', name: 'qEndDate', vtype: 'daterange', startDateField: 'qStartDate', format: 'Y年m月d日',
                        listeners: { afterrender: function () { this.setValue(new Date()); } }
                    }, '-',
                    { scale: 'medium', text: '搜索', icon: "/Admin/style/24Icons/jiancha.png", name: 'btnSearch',
                        handler: function () { Ext.getCmp('gpConsume').getQuery(); }
                    }
                ]

        });

        return grid;
    },
    run: function () {
        var me = this;
        me.center = me.getGrid();
        me.viewport.add([me.center]);
    }
});